package time20151112;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Item_To_User {
    Map<String, ArrayList<String>> item_to_user = new HashMap<String, ArrayList<String>>();
    String filepath = "d:/ratings_train_test.txt";
    File file=new File(filepath);
    public Item_To_User() throws IOException{
        //String filepath = "d:/ratings_train_test.txt";
        String encoding="GBK";
        //File file=new File(filepath);
        
        if(file.isFile() && file.exists()) {
            InputStreamReader read = new InputStreamReader(
            new FileInputStream(file),encoding);
            BufferedReader bufferedReader = new BufferedReader(read);
            String line = null;
            
            while((line = bufferedReader.readLine()) != null){
                String[] str =  line.split("::");
                String userid = str[0];
                String movieid = str[1];
                String rate = str[2];
                    if (!item_to_user.containsKey(str[0])) {                        
                        ArrayList<String> array = new ArrayList<String>();
                        array.add(movieid + ":" + rate);
                       
                        item_to_user.put(userid, array);                        
                    }                   
                    else {
                        item_to_user.get(userid).add(movieid + ":" + rate);
                    }                   
            }
            read.close();
        }
    
    FileWriter fw;
    PrintWriter pw;
    Set set = item_to_user.keySet();
    Iterator<String> it = set.iterator();
    File file1 = new File("D:\\user_to_item.txt");
    if(!file.exists()){
        file.createNewFile();
        fw = new FileWriter(file);
        pw = new PrintWriter(fw);
        while (it.hasNext())
        {
            String key = it.next();
        
            ArrayList<String> st = item_to_user.get(key);
        
            for(int i = 0; i < st.size() ;i ++)
            {
                
                pw.println(st.get(i));
        
            }   
        }
    pw.close();

    }
}

    
    public ArrayList<String> getdata(String userid)
    {
        return item_to_user.get(userid);
    }
    
    public static void main(String[] args) throws IOException {
        Item_To_User itu =new Item_To_User();
        System.out.println(itu.item_to_user);
       
        ArrayList<String> array = new ArrayList<String>();
        System.out.println("Please input the movieID:");
        
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        array = itu.getdata(str);
        for (int i = 0; i < array.size(); i++) {
            if(i % 2 == 0)
            {
                String[] query =  array.get(i).split(":");
                System.out.println("Userid = " + query[0]  +"  Neighbour is :");
                System.out.println(itu.getdata(query[1]));
            }
        }
    }    
}
